﻿@model DoeLibsMVC.Models.EditUserModel
@{
    ViewBag.Title = "Controll";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2 class="centeredHeaderText">My account</h2>
<div class="formDesignDiv">

    @using (Html.BeginForm("Controll", "User", FormMethod.Post, new { id = "userControllForm", enctype = "application/x-www-form-urlencoded" }))
    {
        <table class="formTable">
            <tbody>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.EMail)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.EMail, new { @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.EMail)
                        </div>
                    </td>
                </tr>

                @if (DoeLibsMVC.Security.SecurityHelper.currentUserIsInStaffGroup())
                {
                    <tr>
                        <td>
                            @Html.LabelFor(model => model.Room)
                        </td>
                        <td>
                            <div>
                                @Html.TextBoxFor(model => model.Room, new { @class = "generalTxtBox" })
                                @Html.ValidationMessageFor(model => model.Room)
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            @Html.LabelFor(model => model.UniPhone)
                        </td>
                        <td>
                            <div>
                                @Html.TextBoxFor(model => model.UniPhone, new { @class = "generalTxtBox" })
                                @Html.ValidationMessageFor(model => model.UniPhone)
                            </div>
                        </td>
                    </tr>
                }
                <tr>
                    <td>
                        @Html.LabelFor(model => model.NewPassword)
                    </td>
                    <td>
                        <div>
                            @Html.PasswordFor(model => model.NewPassword, new { @class = "generalTxtBox", placeholder = "leave empty if you don't want to change it" })
                            @Html.ValidationMessageFor(model => model.NewPassword)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.NewPasswordConfirm)
                    </td>
                    <td>
                        <div>
                            @Html.PasswordFor(model => model.NewPasswordConfirm, new { @class = "generalTxtBox", placeholder = "leave empty if you don't want to change it" })
                            @Html.ValidationMessageFor(model => model.NewPasswordConfirm)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.OldPassword)
                    </td>
                    <td>
                        <div>
                            @Html.PasswordFor(model => model.OldPassword, new { @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.OldPassword)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="save" class="generalBtn" />
                    </td>
                </tr>
            </tbody>
        </table>

    }

    @if (!DoeLibsMVC.Security.SecurityHelper.currentUserIsInStaffGroup())
    {
        <text>Your account is going to expire at @User.UserExpirationDate.Value.ToShortDateString()</text>
        <text><a href="#" data-toggle="modal" data-target="#renewExpireDateModal" style="color: blue">here</a> to send a request to renew you expire date.</text>
    }
</div>

@if (!DoeLibsMVC.Security.SecurityHelper.currentUserIsInStaffGroup())
{
    @Html.Action("RenewExpireDate", "User");
}

<script type="text/javascript">


    $("#userControllForm").submit(function () {
        console.log("data", getFormData($('#userControllForm')));

        $.ajax({
            url: "/api/User/@User.UserId",
            type: "PUT",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", getAuthorizationHeader('@User.UserId', ''));
            },
            dataType: "json",
            data: getFormData($('#userControllForm'))
        }).always(function (data) {
            clearAllErrorFields('userControllForm');
            console.log(data);

            if (data.status == 400) {
                console.log("bad request");

                addErrorsToFields("model.", data.responseJSON);
            } else if (data.status == 200) {
                //success
                setHttpMessageDynamic("Changes saved successfull", true);
            } else {
                //servererror, etc.
                setHttpMessageDynamic("Technical problems. Please try again", false);
            }
        });

        return false;
    });

</script>

